#include "head.h"
#include "uart.h"
#include <unistd.h>
#include "do_server.h"


void send_to_M0()
{
	char buf[64]="1";
	char *port = "/dev/serial/by-path/pci-0000:02:00.0-usb-0:2.2:1.0-port0";
	//char *port = "/dev/ttyUSB0";

	int fd;
	int ret;


	//打开串口并返回设备描述符
	fd = uart_open(port);
	if(fd == -1)
	{
		perror("failed to uart_open()\n");
		exit(EXIT_FAILURE);
	}


	//初始化
	if((uart_init(fd,115200,0,8,1,'N')) == -1)
	{ 
		perror("failed to uart_init()\n");
		exit(EXIT_FAILURE);
	}



	//发送数据
	if( (uart_send(fd,buf,1)) == -1)
	{
		perror("failed to uart_send()\n");
		exit(EXIT_FAILURE);
	}
	
	usleep(1000);

	uart_close(fd);
}

int main(int argc, const char *argv[])
{
	int sfd,cfd,ret;
	struct sockaddr_in svr_addr,cli_addr;
	socklen_t len = sizeof(cli_addr);
	ssize_t rbytes = 0; //实际接受的字节数
	ssize_t sbytes = 0; //实际发送的字节数
	pthread_t tid;//存储线程id
	int err; //存储错误码
	int i;

    //数据库相关
	sqlite3 *pdb;

	if(sqlite3_open("Mydata.db",&pdb) != SQLITE_OK)
	{
		fprintf(stderr,"sqlite3 open Mydata.db failed\n");
		exit(EXIT_FAILURE);
	}

	//send_to_M0();


	char buf[256] = {0};
	//STEP1: 创建流式套接字
	sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd == -1)
	{
		perror("failed to socket()\n");
		exit(EXIT_FAILURE);
	}

	//STEP2:绑定ip地址与端口号
	bzero(&svr_addr,sizeof(svr_addr));
	svr_addr.sin_family = AF_INET;
	svr_addr.sin_port = htons(8080);
	svr_addr.sin_addr.s_addr = inet_addr("192.168.4.73");

	ret = bind(sfd,(const struct sockaddr*)&svr_addr,sizeof(svr_addr));
	if(ret == -1)
	{
		perror("failed to bind()\n");
		exit(EXIT_FAILURE);
	}

	//STEP3:建立监听队列，让套接字进入监听状态
	ret = listen(sfd,10);
	if(ret == -1)
	{
		perror("failed to listen()\n");
		exit(EXIT_FAILURE);
	}


	//暂时使用 多路复用方法
	int epfd = epoll_create(1);
	if(epfd == -1)
	{
		perror("failed to epoll_create()\n");
		exit(EXIT_FAILURE);
	}
	struct epoll_event ev;
	ev.events = EPOLLIN;
	ev.data.fd = sfd;
	if((ret = epoll_ctl(epfd,EPOLL_CTL_ADD,sfd,&ev)) == -1)
	{
		perror("failed to epoll_ctl()\n");
		exit(EXIT_FAILURE);
	}

	struct epoll_event res_ev[10];
	while(1)
	{
		//判断对应文件描述符是否就绪
		if((ret = epoll_wait(epfd,res_ev,10,1000)) == -1)
		{
			perror("epoll_wait()\n");
			exit(EXIT_FAILURE);
		}else if(ret == 0)
		{
			printf("Timeout\n");
		}else if(ret > 0)
		{
			for(i = 0; i < ret; i++)
			{
				//表示sfd就绪
				if(res_ev[i].data.fd == sfd)
				{
					//建立连接 返回新的套接字描述符
					if((cfd = accept(sfd,(struct sockaddr*)&cli_addr,&len)) == -1)
					{
						perror("failed to accept()\n");
						exit(EXIT_FAILURE);
					}

					printf("ip:%s,port:%d connect\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));

					struct epoll_event cli_ev;
					//监测sfd是否可读
					cli_ev.events = EPOLLIN;
					cli_ev.data.fd =cfd;
					epoll_ctl(epfd,EPOLL_CTL_ADD,cfd,&cli_ev);
				}else
				{
					memset(buf,0,sizeof(buf));

					rbytes = read(res_ev[i].data.fd,buf,sizeof(buf) - 1);
					if(rbytes < 0)
					{
						perror("failed to read()\n");
						exit(EXIT_FAILURE);
					}
					if(rbytes == 0)
					{
						close(res_ev[i].data.fd);
						epoll_ctl(epfd,EPOLL_CTL_DEL,res_ev[i].data.fd,NULL);
				//		printf("ip:%s,port:%d disconnect\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
						break;
					}
                   
					printf("rev buf : %s\n",buf);
#if 1
	//封装json数据
	json_object *my_object;
	char *json_string = NULL;

	my_object = json_object_new_object();
	json_object_object_add(my_object,"stateCode",json_object_new_int(1));
	json_string = (char *)json_object_to_json_string(my_object);

	json_object_put(my_object);

	int n = 0;
	n = write(res_ev[i].data.fd,json_string,strlen(json_string));
	if(n < 0)
	{
		perror("failed to send\n");
		return 0;
	}
#endif
					do_server(res_ev[i].data.fd,buf,pdb);
#if 0
					json_object *new_obj = NULL;
					struct json_object *child_object = NULL;

					new_obj = json_tokener_parse(buf);
					child_object = json_object_object_get(new_obj,"报文类型");

					printf("报文类型:%s\n",json_object_get_string(child_object));
#else
			     	//char temp[] = "ok";

				//	write(res_ev[i].data.fd,temp,strlen(temp));
#endif


				}
		    }
		}
	}

	close(sfd);
	
	return 0;
}
